#include "building.h"
#include <iostream>


#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE  0x809D
#endif

Building::Building(QWidget *parent) : QGLWidget(parent)
{



}

void Building::setCamera(QVector<double> vector)
{
    camPosx=vector.value(0);
    camPosy=vector.value(1);
    camPosz=vector.value(2);
    camViewx=vector.value(3);
    camViewy=vector.value(4);
    camViewz=vector.value(5);
    camUpx=vector.value(6);
    camUpy=vector.value(7);
    camUpz=vector.value(8);



}

void Building::setObject(GLuint object)
{

    myobject=glGenLists(1);
    myobject = object;

    if(glIsList(myobject)==GL_TRUE)
    {
        std::cout<< "good \n";
    }
}

void Building::initializeGL()
{
    QGLWidget::initializeGL();

    //background
    glClearColor(0.0f,0.0f,0.0f,0.0f);


    //place light
    glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHT0);
    glEnable(GL_MULTISAMPLE);

    //    //position light
    //    static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
    //    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);

    //ambient light
    static GLfloat global_ambient[4] = {0.5, 0.5, 0.5, 1.0};
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT,global_ambient);


    XMLParser parser(this);
    parser.parse();


}

void Building::resizeGL(int w, int h)
{
    if((w<=0)||(h<=0))
        return;

    //setViewPort
    glViewport(0,0,w,h);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();
    GLdouble aspect_ratio=(GLdouble)w/(GLdouble)h;

   // gluPerspective(150,aspect_ratio,1,2000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void Building::paintGL()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    gluLookAt(camPosx ,camPosy ,camPosz,
              camViewx,camViewy,camViewz,
              camUpx, camUpy, camUpz );

    glDisable(GL_LIGHTING);

    if(glIsList(myobject)==GL_TRUE)
    {glLoadIdentity();
        glBegin(GL_LINES);
        glColor3f(1.0, 0.0, 0.0);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(10.0, 0.0, 0.0);
        glColor3f(0.0, 1.0, 0.0);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.0, 10.0, 0.0);
        glColor3f(0.0, 0.0, 1.0);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.0, 0.0, 10.0);
        glEnd();

    }
    else if(glIsList(myobject)==GL_TRUE)
    {
        std::cout<< "h\n";
    }


            glCallList(myobject);

    //drawRectangle(2.0, 2.0, 2.0);

    glEnable(GL_LIGHTING);


}

void Building::drawRectangle(GLfloat b, GLfloat h, GLfloat d)
{
    glBegin(GL_QUADS);

    glColor3f(1, 0, 0);
    glVertex3f(0, 0 ,0);
    glVertex3f(b, 0, 0);
    glVertex3f(b, h, 0);
    glVertex3f(0, h, 0);

    glColor3f(0, 1, 0);
    glVertex3f(0, 0, 0);
    glVertex3f(0, 0, d);
    glVertex3f(0, h, 0);
    glVertex3f(0, h, d);

    glColor3f(0, 0, 1);
    glVertex3f(0, 0, 0);
    glVertex3f(0, 0, d);
    glVertex3f(b, 0, d);
    glVertex3f(b, 0, 0);

    glColor3f(1, 0, 0);
    glVertex3f(0, 0, d);
    glVertex3f(b, 0, d);
    glVertex3f(b, h, d);
    glVertex3f(0, h, d);


    glVertex3f(0 ,0 ,d);
    glVertex3f(b, 0, d);
    glVertex3f(b, h, d);
    glVertex3f(0, h, d);


    glVertex3f(0, h, 0);
    glVertex3f(0, h, d);
    glVertex3f(b, h, d);
    glVertex3f(b, h, 0);

    glEnd();
    glFlush();

    /*glBegin(GL_QUADS);
    glNormal3f( 0.0F, 0.0F, 1.0F);
    glVertex3f( 0.5F, 0.5F, 0.5F);
    glVertex3f(-0.5F, 0.5F, 0.5F);
    glVertex3f(-0.5F,-0.5F, 0.5F);
    glVertex3f( 0.5F,-0.5F, 0.5F);

    glNormal3f( 0.0F, 0.0F,-1.0F);
    glVertex3f(-0.5F,-0.5F,-0.5F);
    glVertex3f(-0.5F, 0.5F,-0.5F);
    glVertex3f( 0.5F, 0.5F,-0.5F);
    glVertex3f( 0.5F,-0.5F,-0.5F);

    glNormal3f( 0.0F, 1.0F, 0.0F);
    glVertex3f( 0.5F, 0.5F, 0.5F);
    glVertex3f( 0.5F, 0.5F,-0.5F);
    glVertex3f(-0.5F, 0.5F,-0.5F);
    glVertex3f(-0.5F, 0.5F, 0.5F);

    glNormal3f( 0.0F,-1.0F, 0.0F);
    glVertex3f(-0.5F,-0.5F,-0.5F);
    glVertex3f( 0.5F,-0.5F,-0.5F);
    glVertex3f( 0.5F,-0.5F, 0.5F);
    glVertex3f(-0.5F,-0.5F, 0.5F);

    glNormal3f( 1.0F, 0.0F, 0.0F);
    glVertex3f( 0.5F, 0.5F, 0.5F);
    glVertex3f( 0.5F,-0.5F, 0.5F);
    glVertex3f( 0.5F,-0.5F,-0.5F);
    glVertex3f( 0.5F, 0.5F,-0.5F);

    glNormal3f(-1.0F, 0.0F, 0.0F);
    glVertex3f(-0.5F,-0.5F,-0.5F);
    glVertex3f(-0.5F,-0.5F, 0.5F);
    glVertex3f(-0.5F, 0.5F, 0.5F);
    glVertex3f(-0.5F, 0.5F,-0.5F);
    glEnd();*/
}
